/**
 * 
 */
package com.xb.concurrent.threadpool;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xb.concurrent.hashmap.ReadTask;

/**
 * @author kexue
 *
 */
public class ThreadPoolDemo implements ExecutorService {
	private Logger logger = LoggerFactory.getLogger(ThreadPoolDemo.class);
	
	// 工作线程列表(消费者)
	private List<WorkThread> workThreads;
	
	// 等待任务队列(生产者)
	private BlockingQueue<Runnable> tasks = new LinkedBlockingQueue<Runnable>();
	
	private int maxThreadPoolSize;
	 private volatile long keepAliveTime=10*1000;//10ms

	public ThreadPoolDemo(int maxThreadPoolSize) {
		this.maxThreadPoolSize = maxThreadPoolSize;
		this.workThreads = new ArrayList<WorkThread>();

		// 启动工作线程列表.
		for (int i = 0; i < maxThreadPoolSize; i++) {
			WorkThread workThread = new WorkThread();
			this.workThreads.add(workThread);
			workThread.thread.start();
		}
	}

	public void execute(Runnable command) {
		if (command != null) {
			try {
				tasks.put(command);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 工作者线程类.
	 * 
	 * @author kexue
	 *
	 */
	class WorkThread implements Runnable {
		private Thread thread;
		private boolean isStop = false;

		public WorkThread() {
			this.thread = new Thread(this);// 初始化线程
		}

		public void run() {
			// TODO
			Runnable task = null;
			while (isStop == false) {
				try {
					task = tasks.poll(keepAliveTime, TimeUnit.NANOSECONDS); //10ms等待,获取队列的第一个元素
					if (task != null) {
						//执行业务逻辑
						task.run();		
					}
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

		public void setStop(boolean isStop) {
			this.isStop = isStop;
		}
	}

	 
	public void shutdown() {
		shutdownNow();
	}

	 
	public List<Runnable> shutdownNow() {
		tasks.clear();
		for(WorkThread thread:workThreads){
			thread.setStop(true);
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.concurrent.ExecutorService#isShutdown()
	 */
	public boolean isShutdown() {
		// TODO Auto-generated method stub
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.concurrent.ExecutorService#isTerminated()
	 */
	public boolean isTerminated() {
		// TODO Auto-generated method stub
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.concurrent.ExecutorService#awaitTermination(long,
	 * java.util.concurrent.TimeUnit)
	 */
	public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
		// TODO Auto-generated method stub
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.concurrent.ExecutorService#submit(java.util.concurrent.
	 * Callable)
	 */
	public <T> Future<T> submit(Callable<T> task) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.concurrent.ExecutorService#submit(java.lang.Runnable,
	 * java.lang.Object)
	 */
	public <T> Future<T> submit(Runnable task, T result) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.concurrent.ExecutorService#submit(java.lang.Runnable)
	 */
	public Future<?> submit(Runnable task) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.concurrent.ExecutorService#invokeAll(java.util.Collection)
	 */
	public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.concurrent.ExecutorService#invokeAll(java.util.Collection,
	 * long, java.util.concurrent.TimeUnit)
	 */
	public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
			throws InterruptedException {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.concurrent.ExecutorService#invokeAny(java.util.Collection)
	 */
	public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.concurrent.ExecutorService#invokeAny(java.util.Collection,
	 * long, java.util.concurrent.TimeUnit)
	 */
	public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
			throws InterruptedException, ExecutionException, TimeoutException {
		// TODO Auto-generated method stub
		return null;
	}

}
